package
{
	/**
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-11-30上午01:59:40
	 * */
	import event.UIEditsEventName;
	
	import fl.controls.Button;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import kingsAsLib.events.EventManager;
	import kingsAsLib.events.KEvents;

	public class MenuUI extends Sprite
	{
		static private var menuUi:MenuUI=new MenuUI();

		private var btnArr:Vector.<Button>=new Vector.<Button>;

		private var btnCreatArr:Array;

		public function MenuUI()
		{
			super();

			btnCreatArr=[]

			btnCreatArr.push({label:"导入图片", name:"importPic"});
			btnCreatArr.push({label:"增加层", name:"addLayer"});
			btnCreatArr.push({label:"锁定层", name:"lockLayer"});
			btnCreatArr.push({label:"删除层", name:"deleteLayer"});
			btnCreatArr.push({label:"生成代码",name:"savebtn"});
			btnCreatArr.push({label:"退出", name:"exitbtn"});
			btnCreatArr.push({label:"帮助", name:"helpbtn"});


			for (var i:int=0; i < btnCreatArr.length; i++)
			{
				var _btn:Button=new Button();
				_btn.width=70;
				_btn.label=btnCreatArr[i].label;
				_btn.name=btnCreatArr[i].name;
				addChild(_btn);
				btnArr.push(_btn);
				if (i > 0)
				{
					btnArr[i].x=btnArr[i - 1].x + btnArr[i - 1].width + 10;
				}
				else
				{
					btnArr[i].x=0;
				}
				btnArr[i].addEventListener(MouseEvent.CLICK, btnClickHandle);
			}

			initListen();
		}

		private function initListen():void
		{
			// 
			EventManager.addEventListener(UIEditsEventName.CurrentLayerChange, currentLayerChangeHandle);

		}

		private function currentLayerChangeHandle(e:KEvents):void
		{
			// 
			var sp:Sprite=e.getData();
			if (sp.mouseEnabled)
			{
				getBtn("lockLayer").label="锁定层"
			}
			else
			{
				getBtn("lockLayer").label="解锁层"
			}
		}

		private function getBtn(_name:String):Button
		{
			for (var i:int=0; i < btnArr.length; i++)
			{
				if (btnArr[i].name == _name)
				{
					return btnArr[i];
				}
			}
			return null;
		}

		protected function btnClickHandle(e:MouseEvent):void
		{
			var btn:Button=Button(e.target);
			switch (btn.name)
			{
				case "importPic":
				{
EventManager.dispatchEvent(new KEvents(UIEditsEventName.LoadPic));
					break;
				}
				case "lockLayer":
				{
					EventManager.dispatchEvent(new KEvents(UIEditsEventName.lockLayer));
					break;
				}
				case "addLayer":
				{
					addLayerHandle();
					break;
				}
				case "deleteLayer":
				{
					deleteLayerHandle();
					break;
				}
				case "savebtn":
				{
					EventManager.dispatchEvent(new KEvents(UIEditsEventName.saveCodeWinShow, true));
					break;
				}
				case "exitbtn":
				{

					break;
				}
				case "helpbtn":
				{

					break;
				}

				default:
				{
					break;
				}
			}
		}

		private function deleteLayerHandle():void
		{
			// 
			EventManager.dispatchEvent(new KEvents(UIEditsEventName.deletLayer));
		}

		private function addLayerHandle():void
		{
			// 
			EventManager.dispatchEvent(new KEvents(UIEditsEventName.addLayer));
		}

		static public function instance():MenuUI
		{
			return menuUi;
		}
	}

	//end class

}



